/**
 * The Prophecy
 * java based lightweight texas hold'em poker software base
 * http://code.google.com/p/theprophecy/
 */

package org.poker.prophecy.core;

import java.util.Vector;

/**
 * @author bg
 */

public class WinnerDeterminer {

    Vector<Vector<PokerHand>> vector = null;
    
    public WinnerDeterminer(Vector<PokerHand> pokerHands){
        //Erst primitiv nach bester Hand Sortieren
        Vector<PokerHand> sortedHands = new Vector<PokerHand>();
        int times = pokerHands.size();
        int aktSize = pokerHands.size();
        PokerHand aktBestHand = null;
        for (int i = 0; i < times; i++) {
            aktSize = pokerHands.size();
            aktBestHand = pokerHands.elementAt(0);
            for (int j = 0; j < aktSize; j++) {
                if (PokerHandComparer.compare(aktBestHand, pokerHands.elementAt(j)) == 0){
                    //NOOP da aktBestHand schon besser ist
                }
                else{
                    if (PokerHandComparer.compare(aktBestHand, pokerHands.elementAt(j)) == 1){
                        aktBestHand = pokerHands.elementAt(j);
                    }
                    else{
                        //NOOP da gleichwertig
                    }
                }
            }
            sortedHands.addElement(aktBestHand);
            pokerHands.removeElement(aktBestHand);
        }
        
        //System.out.println("SHs: "+sortedHands);
        
        Vector<Vector<PokerHand>> returnVector = new Vector<Vector<PokerHand>>();
        Vector<PokerHand> aktCategory = new Vector<PokerHand>();
        PokerHand lastHand = sortedHands.elementAt(0);
        PokerHand aktHand = null;
        aktCategory.addElement(lastHand);
        for (int i = 1; i < sortedHands.size(); i++) {
            aktHand = sortedHands.elementAt(i);
            if (PokerHandComparer.compare(aktHand, lastHand) == 2){
                aktCategory.addElement(aktHand);
                lastHand = aktHand;
                if (i == (sortedHands.size()-1)){
                    returnVector.addElement(aktCategory);
                }
            }
            else{
                returnVector.addElement(aktCategory);
                aktCategory = new Vector<PokerHand>();
                aktCategory.addElement(aktHand);
                lastHand = aktHand;
                if (i == (sortedHands.size()-1)){
                    returnVector.addElement(aktCategory);
                }
            }
        }
        vector = returnVector;
        //System.out.println(returnVector);
    }
    
    public int getNrOfWinningCategories(){
        return vector.size();
    }
    
    public Vector<PokerHand> getWinners(){
        return vector.elementAt(0);
    }
    
    public int getCategoryNumberForHand(PokerHand pokerHand){
        Vector<PokerHand> aktKategorie = null;
        for (int i = 0; i < vector.size(); i++) {//Kategorie
            aktKategorie = vector.elementAt(i);
            for (int j = 0; j < aktKategorie.size(); j++) {
                if (aktKategorie.elementAt(j).equals(pokerHand)){
                    return i+1;
                }
            }
        }
        return -1;
    }
}
